package com.huikeportal.service.impl;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huikeportal.dao.SquareMemberMapper;
import com.huikeportal.model.KequnAnalysisModel;
import com.huikeportal.model.ReportGraphData;
import com.huikeportal.model.ReportMemberArrve;
import com.huikeportal.model.UserInfo;
import com.huikeportal.service.FootfallGlobalService;
import com.huikeportal.service.SquareMemberService;

@Service("SquareMemberService")
public class SquareMemberServiceImpl implements SquareMemberService{

	
	
	@Autowired
	SquareMemberMapper squareMemberMapper;
	
	@Autowired
	private FootfallGlobalService footfallGlobalService;
	

	public String getChinaWeek(int weekInt){
		String weekStr="星期一";
		switch (weekInt) {
		case 1:
			weekStr="星期日";
			break;
		case 2:
			weekStr="星期一";
			break;
		case 3:
			weekStr="星期二";
			break;
		case 4:
			weekStr="星期三";
			break;
		case 5:
			weekStr="星期四";
			break;
		case 6:
			weekStr="星期五";
			break;
		case 7:
			weekStr="星期六";
			break;
		default:
			break;
		}
		return weekStr;
	}
	
	/**
	 * 数字千分位
	 * @param text
	 * @return
	 */
	public  String fmtMicrometer(String text)  
    {  
        DecimalFormat df = null;  
        if(text.indexOf(".") > 0)
        {  
            if(text.length() - text.indexOf(".")-1 == 0)  
            {  
                df = new DecimalFormat("###,##0.");  
            }else if(text.length() - text.indexOf(".")-1 == 1)  
            {  
                df = new DecimalFormat("###,##0.0");  
            }else  
            {  
                df = new DecimalFormat("###,##0.00");  
            }  
        }else   
        {  
            df = new DecimalFormat("###,##0");  
        }  
        double number = 0.0;  
        try {  
             number = Double.parseDouble(text);  
        } catch (Exception e) {  
            number = 0.0;  
        }  
        return df.format(number);  
    }
	
	/**前台获取图表数据  */
	public StringBuffer getGlobalTable(List<ReportGraphData> list,String timeType_table,UserInfo userInfo,String siteId) throws Exception {
		
		//计算table列宽
		int rowNum = 5;
		float rowWidth = 0;
		
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
		StringBuffer stringTable=new StringBuffer();
		String thxingqi = "";
		if("day".equals(timeType_table)){
			rowWidth = (float) ((100-5.4)/(rowNum));
			thxingqi = "<th  style='width: "+rowWidth+"%;'>星期</th>";
		}else{
			rowWidth = (float) ((100-5.4)/(rowNum)-1);
			thxingqi = "";
		}
		stringTable.append("<table class='indextable xyliebiao widthall'>");
		stringTable.append("<tr><th class='xykongbai'></th>"
								+"<th class='xyxuhao'>编号</th>"
								+"<th  style='width: "+rowWidth+"%;'>日期</th>");
		stringTable.append(thxingqi);
        stringTable.append("<th style='width: " + rowWidth +
            "%;'>顾客数（人）</th>");

        stringTable.append("<th style='width: " + rowWidth +
            "%;'>到达会员人数（人）</th>");

		stringTable.append("<th class='xykongbai'></th></tr>");
		 for (int i = 0; i < list.size(); i++){
			String tdxingqi = "";
			String tdriqi = "";
			if("hour".equals(timeType_table)){
				tdxingqi = "";
				tdriqi = "<td>"+list.get(i).getDatatime()+"</td>";
			}else if("day".equals(timeType_table)){
				Calendar cl=Calendar.getInstance();
				cl.setTime(sf.parse(list.get(i).getDatatime()));
				int weekInt=cl.get(Calendar.DAY_OF_WEEK);
				tdxingqi = "<td>"+getChinaWeek(weekInt)+"</td>";
				tdriqi = "<td>"+list.get(i).getDatatime()+"</td>";
			}else if("week".equals(timeType_table)){
				tdxingqi = "";
				tdriqi = "<td>"+list.get(i).getYear()+"-"+list.get(i).getWeek()+"</td>";
			}else if("month".equals(timeType_table)){
				tdxingqi = "";
				tdriqi = "<td>"+list.get(i).getYear()+"-"+list.get(i).getMonth()+"</td>";
			}else{
				tdxingqi = "";
				tdriqi = "<td>"+list.get(i).getYear()+"</td>";
			}
			stringTable.append("<tr>");
			stringTable.append("<td class='xykongbai'></td>"
					+"<td class='xyxuhao'>"+(i + 1)+"</td>");
			stringTable.append(tdriqi);
			stringTable.append(tdxingqi);
            stringTable.append("<td>" +
                fmtMicrometer(list.get(i).getCustomerNum()) + "</td>");
            stringTable.append("<td>" +
                fmtMicrometer(list.get(i).getMemNum()) + "</td>");

			stringTable.append("<td></td>"); 
			stringTable.append("</tr>");
         }
		 stringTable.append("</table>");
		return stringTable;
	}
	
	public HashMap<String, Double> getPeiNweOld(List<ReportGraphData> list){
		double  newCustomerNum = 0;
		double  oldCustomer = 0;
		for (int i = 0; i < list.size(); i++){
			String newCustomerNumStr = list.get(i).getNewCustomerNum();
			if(newCustomerNumStr != null && !"".equals(newCustomerNumStr)){
				newCustomerNum = newCustomerNum + Double.parseDouble(newCustomerNumStr);
			}else{
				newCustomerNum = newCustomerNum + 0;
			}
			String oldCustomerStr = list.get(i).getOldCustomer();
			if(oldCustomerStr != null && !"".equals(oldCustomerStr)){
				oldCustomer = oldCustomer + Double.parseDouble(oldCustomerStr);
			}else{
				oldCustomer = oldCustomer + 0;
			}
		 }
		HashMap<String, Double> retMap = new HashMap<String, Double>();
		retMap.put("newCustomerNum", newCustomerNum);
		retMap.put("oldCustomer", oldCustomer);
		return retMap;
	}

	public HashMap<String, Double> peiArrMember(List<ReportGraphData> list){
		
		//n天到达会员的平均值
		double arrMemberNumToal = 0;
		for(ReportGraphData reportGraphData:list){
			String arrMember = reportGraphData.getMemNum();
			double arrMemberNum = (arrMember == null ||"".equals(arrMember))?0:Double.parseDouble(arrMember);
			arrMemberNumToal = arrMemberNumToal + arrMemberNum;
		}
		double arrMemberNumToalAvg = arrMemberNumToal/list.size();
		
		
		
		//最后一天的会员总数
		ReportGraphData reportGraphDataLast = new ReportGraphData();
		if(list != null && list.size()>0){
			reportGraphDataLast = list.get(list.size()-1);
		}
		String totalMember = reportGraphDataLast.getMemTotalNum();
		
		
		double totalMemberNum = (totalMember == null ||"".equals(totalMember))?0:Double.parseDouble(totalMember);
		
		double noArrMemberNum = totalMemberNum - arrMemberNumToalAvg;
		
		HashMap<String, Double> retMap = new HashMap<String, Double>();
		retMap.put("arrMemberNum",arrMemberNumToalAvg);
		retMap.put("noArrMemberNum",noArrMemberNum);
		
		return retMap;
	}
	
	public HashMap<String, Double> peiMemberFenbu(List<ReportGraphData> list){

		double arrMemberNumToal = 0;
		double customerNumNumToal = 0;
		for(ReportGraphData reportGraphData:list){
			String arrMember = reportGraphData.getMemNum();
			String customerNum = reportGraphData.getCustomerNum();//顾客数
			double arrMemberNum = (arrMember == null ||"".equals(arrMember))?0:Double.parseDouble(arrMember);
			double customerNumNum = (customerNum == null ||"".equals(customerNum))?0:Double.parseDouble(customerNum);
			arrMemberNumToal = arrMemberNumToal + arrMemberNum;
			customerNumNumToal = customerNumNumToal + customerNumNum;
		}
		
		double notMemNumNum =customerNumNumToal - arrMemberNumToal;//非会员人数
		
		HashMap<String, Double> retMap = new HashMap<String, Double>();
		retMap.put("memNumNum",arrMemberNumToal);//会员人数
		retMap.put("notMemNumNum",notMemNumNum);//非会员人数
		
		return retMap;
	
	}
	
	public List<ReportMemberArrve> getMemberArrve(HashMap<String , Object> map){

		List<ReportMemberArrve> resultList=new ArrayList<ReportMemberArrve>();
		String timeType="";
		if(map.get("timeType")!=null && !map.get("timeType").toString().equals("")){
			timeType=map.get("timeType").toString();
		}
		String timeType_table="";
		if(map.get("timeType_table")!=null && !map.get("timeType_table").toString().equals("")){
			timeType_table=map.get("timeType_table").toString();
		}
		String from="";
		if(map.get("startDate")!=null && !map.get("startDate").toString().equals("")){
			from=map.get("startDate").toString();
		}
		String to="";
		if(map.get("endDate")!=null && !map.get("endDate").toString().equals("")){
			to=map.get("endDate").toString();
		}
		String siteId="";
		if(map.get("siteId")!=null && !map.get("siteId").toString().equals("")){
			siteId=map.get("siteId").toString();
		}
		UserInfo userInfo=null;
		if(map.get("userInfo")!=null){
			userInfo=(UserInfo) map.get("userInfo");
		}
		//根据日周月年的不同需要的参数map
		HashMap<String, Object> pramMap1 = new HashMap<String, Object>();
		try {
			pramMap1 = footfallGlobalService.getparam(timeType_table,timeType,from,to);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if(siteId.equals("")){
//			pramMap1.put("siteid",siteAllInfo.getSiteid());//放入场所id
		}else{
			pramMap1.put("siteid",siteId);//放入场所id
		}
		pramMap1.put("userId",userInfo.getUserId());
		resultList = squareMemberMapper.getMemberArrve(pramMap1);
		return resultList;
	}
	
	public HashMap<String, Double>  getMemberArrveSex(List<ReportMemberArrve> list){
		
		double man = 0;
		double woman = 0;
		
		for(ReportMemberArrve reportMemberArrve :list){
			String manTemp = reportMemberArrve.getMan();
			String womanTemp = reportMemberArrve.getWoman();
			man = man + ((manTemp == null || "".equals(manTemp))?0:Double.parseDouble(manTemp));
			woman = woman + ((womanTemp == null || "".equals(womanTemp))?0:Double.parseDouble(womanTemp));
		}
		HashMap<String, Double> retMap = new HashMap<String, Double>();
		retMap.put("man",man);
		retMap.put("woman",woman);
		return retMap;
	}
	
	public HashMap<String, Double>  getMemberArrveAge(List<ReportMemberArrve> list){
		double sixteenLow = 0;
		double sixteenToTwentyFive = 0;
		double TwentySixThirtyFive = 0;
		double ThirtySixToFortyFive = 0;
		double FortyFiveUp = 0;
		for(ReportMemberArrve reportMemberArrve :list){
			String sixteenLowTemp = reportMemberArrve.getSixteenLow();
			String sixteenToTwentyFiveTemp = reportMemberArrve.getSixteenToTwentyFive();
			String twentySixThirtyFiveTemp = reportMemberArrve.getTwentySixThirtyFive();
			String thirtySixToFortyFiveTemp = reportMemberArrve.getThirtySixToFortyFive();
			
			String fortysixToFiftyFiveTemp = reportMemberArrve.getFortysixToFiftyFive();
			String fiftysixUpTemp = reportMemberArrve.getFiftysixUp();
			
			sixteenLow = sixteenLow + ((sixteenLowTemp == null || "".equals(sixteenLowTemp))?0:Double.parseDouble(sixteenLowTemp));
			sixteenToTwentyFive = sixteenToTwentyFive + ((sixteenToTwentyFiveTemp == null || "".equals(sixteenToTwentyFiveTemp))?0:Double.parseDouble(sixteenToTwentyFiveTemp));
			TwentySixThirtyFive = TwentySixThirtyFive + ((twentySixThirtyFiveTemp == null || "".equals(twentySixThirtyFiveTemp))?0:Double.parseDouble(twentySixThirtyFiveTemp));
			ThirtySixToFortyFive = ThirtySixToFortyFive + ((thirtySixToFortyFiveTemp == null || "".equals(thirtySixToFortyFiveTemp))?0:Double.parseDouble(thirtySixToFortyFiveTemp));
			
			FortyFiveUp = FortyFiveUp + ((fortysixToFiftyFiveTemp == null || "".equals(fortysixToFiftyFiveTemp))?0:Double.parseDouble(fortysixToFiftyFiveTemp)) +((fiftysixUpTemp == null || "".equals(fiftysixUpTemp))?0:Double.parseDouble(fiftysixUpTemp));
		}
		HashMap<String, Double> retMap = new HashMap<String, Double>();
		retMap.put("sixteenLow",sixteenLow);
		retMap.put("sixteenToTwentyFive",sixteenToTwentyFive);
		retMap.put("TwentySixThirtyFive",TwentySixThirtyFive);
		retMap.put("ThirtySixToFortyFive",ThirtySixToFortyFive);
		retMap.put("FortyFiveUp",FortyFiveUp);
		return retMap;
	}
	public HashMap<String, Double>  getMemberArrveSexAll(List<ReportMemberArrve> list){
		
		double man = 0;
		double woman = 0;
		ReportMemberArrve reportMemberArrve = new ReportMemberArrve();
		if(list != null && list.size()>0){
			reportMemberArrve = list.get(list.size()-1);
		}
		String manTemp = reportMemberArrve.getManAll();
		String womanTemp = reportMemberArrve.getWomanAll();
		man = man + ((manTemp == null || "".equals(manTemp))?0:Double.parseDouble(manTemp));
		woman = woman + ((womanTemp == null || "".equals(womanTemp))?0:Double.parseDouble(womanTemp));
		HashMap<String, Double> retMap = new HashMap<String, Double>();
		retMap.put("man",man);
		retMap.put("woman",woman);
		return retMap;
	}
	
	public HashMap<String, Double>  getMemberArrveAgeAll(List<ReportMemberArrve> list){
		double sixteenLow = 0;
		double sixteenToTwentyFive = 0;
		double TwentySixThirtyFive = 0;
		double ThirtySixToFortyFive = 0;
		double FortyFiveUp = 0;
		ReportMemberArrve reportMemberArrve = new ReportMemberArrve();
		if(list != null && list.size()>0){
			reportMemberArrve = list.get(list.size()-1);
		}
		String sixteenLowTemp = reportMemberArrve.getSixteenLowAll();
		String sixteenToTwentyFiveTemp = reportMemberArrve.getSixteenToTwentyFiveAll();
		String twentySixThirtyFiveTemp = reportMemberArrve.getTwentySixThirtyFiveAll();
		String thirtySixToFortyFiveTemp = reportMemberArrve.getThirtySixToFortyFiveAll();
		
		String fortysixToFiftyFiveTemp = reportMemberArrve.getFortysixToFiftyFiveAll();
		String fiftysixUpTemp = reportMemberArrve.getFiftysixUpAll();
		
		sixteenLow = sixteenLow + ((sixteenLowTemp == null || "".equals(sixteenLowTemp))?0:Double.parseDouble(sixteenLowTemp));
		sixteenToTwentyFive = sixteenToTwentyFive + ((sixteenToTwentyFiveTemp == null || "".equals(sixteenToTwentyFiveTemp))?0:Double.parseDouble(sixteenToTwentyFiveTemp));
		TwentySixThirtyFive = TwentySixThirtyFive + ((twentySixThirtyFiveTemp == null || "".equals(twentySixThirtyFiveTemp))?0:Double.parseDouble(twentySixThirtyFiveTemp));
		ThirtySixToFortyFive = ThirtySixToFortyFive + ((thirtySixToFortyFiveTemp == null || "".equals(thirtySixToFortyFiveTemp))?0:Double.parseDouble(thirtySixToFortyFiveTemp));
		
		FortyFiveUp = FortyFiveUp + ((fortysixToFiftyFiveTemp == null || "".equals(fortysixToFiftyFiveTemp))?0:Double.parseDouble(fortysixToFiftyFiveTemp)) +((fiftysixUpTemp == null || "".equals(fiftysixUpTemp))?0:Double.parseDouble(fiftysixUpTemp));
		HashMap<String, Double> retMap = new HashMap<String, Double>();
		retMap.put("sixteenLow",sixteenLow);
		retMap.put("sixteenToTwentyFive",sixteenToTwentyFive);
		retMap.put("TwentySixThirtyFive",TwentySixThirtyFive);
		retMap.put("ThirtySixToFortyFive",ThirtySixToFortyFive);
		retMap.put("FortyFiveUp",FortyFiveUp);
		return retMap;
	}
}
